<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="ut.data.User" %>
<%@ page import="ut.data.Task" %>
<%@ page import="ut.data.Question" %>
<%@ page import="ut.util.DateTime" %>
<%
	User user = (User)session.getAttribute("user");
	
	int p = 1;
	long tid = 0;
	if(request.getParameter("id") == null){
		return;
	}	
	try{
		tid = Long.parseLong(request.getParameter("id"));
		if(request.getParameter("p") != null){
			p = Integer.parseInt(request.getParameter("p"));
		}
	}catch(NumberFormatException e){
		return;
	}
	
	Task task = Task.getTask(tid);
	if(task == null){
		return;
	}
	
	int maxPage = Question.getMaxQuestionPage(tid, 10);
	if(p > maxPage){
		p = maxPage;
	}
	Question[] questions = Question.getQuestions(tid, 10, p);
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>UT終極一翻！ | 付費案件#<%= tid %>－問與答</title>
	<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
	<link rel="stylesheet" type="text/css" href="/css/popup.css" media="all" />
	<script type="text/javascript" src="/js/Utility.js"></script>
	<script type="text/javascript" src="/js/Message.js"></script>
	<script type="text/javascript" src="/js/Ajax.js"></script>
	<script type="text/javascript" src="/js/Button.js"></script>
	<script type="text/javascript" src="/js/Account.js"></script>
	<script type="text/javascript" src="/js/Task.js"></script>
	<script type="text/javascript" src="/script.js"></script>
</head>
<body onload="init();">
	<div class="content popup">
<%
	if(user == null){	//訪客
%>
		<div class="task block" style="margin-top:0px;">
			<span class="tag top">我要提出問題</span>
			<form class="task-content post" onsubmit="Task.createQuestion(this);return false;">
				<input type="hidden" name="tid" value="<%= tid %>" />
				<textarea name="content" disabled></textarea>
				<div>
					<input class="button" type="submit" value="請先登入以提出問題" disabled
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
			</form>
		</div>
<%
	}else if(user.id != task.employer){	//一般使用者
%>
		<div class="task block" style="margin-top:0px;">
			<span class="tag top">我要提出問題</span>
			<form class="task-content post" onsubmit="Task.createQuestion(this);return false;">
				<input type="hidden" name="tid" value="<%= tid %>" />
				<textarea name="content"></textarea>
				<div>
					<input class="button" type="submit" value="提出問題"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
			</form>
		</div>
<%
	}
%>
		<div class="page">
<%
		StringBuilder pageOptions = new StringBuilder("");
		if(p > 1){
			pageOptions.append("<a href=\"./?page=1\">第一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
		}else{
			pageOptions.append("<span class=\"inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
		}
		int startPage = (p - 2 > 0) ? p - 2 : 1;
		int endPage = startPage + 4;
		if(endPage > maxPage){
			startPage = startPage - (endPage - maxPage);
			endPage = maxPage;
		}
		for(int i = startPage; i <= endPage; i++){
			if(i < 1){
				continue;
			}else if(i == p){
				pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
			}else{
				pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
			}
		}
		if(p < maxPage){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
		}
		out.println(pageOptions.toString());
%>
		</div>
<%
	if(questions[0] == null){
%>
		<div class="task block">
			<div class="block-center">目前沒有任何訊息</div>
		</div>
<%
	}else{
		for(int i = 0; i < questions.length; i++){
			if(questions[i] == null){
				break;
			}
%>
		<div class="task block">
			<span class="tag top blue">
				發問者：
				<span class="opener-link"
					onclick="opener.document.location='/account/member-info.jsp?uid=<%= questions[i].user %>&view=1'">
					<%= User.getUserName(questions[i].user) %>
				</span>
			</span>
			<div class="task-content">
				<div><%= questions[i].content %></div>
				<div class="date">(<%= DateTime.toDateTime(questions[i].time) %>)</div>
			</div>
<%
			if(!questions[i].answer.equals("")){
%>
			<div class="task-content answer">
				<%= questions[i].answer %>
			</div>
			<div class="task-bottom">
				<span class="tag bottom green">案主回應</span>
			</div>
<%
			}else if(user != null && user.id == task.employer){
%>
			<form class="task-content post answer" onsubmit="Task.answerQuestion(this);return false;">
				<input type="hidden" name="qid" value="<%= questions[i].id %>" />
				<textarea name="content"></textarea>
				<div>
					<input class="button" type="submit" value="回應"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
			</form>
<%
			}
%>
		</div>
<%
		}
	}
%>
		<div class="page">
<%
		out.println(pageOptions.toString());
%>		
		</div>
	</div>
</body>
</html>